package Test.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import Test.dao.UserDao;
import Test.httpModel.Users;
import Test.model.Syusers;
import Test.service.UserService;

import common.httpModel.EasyuiDataGrid;
import common.httpModel.EasyuiDataGridJson;
import common.service.impl.BaseServiceImpl;
import common.util.Encrypt;

@Service("tUserService")
public class UserServiceImpl extends BaseServiceImpl implements UserService {
	private UserDao<Syusers> userDao;
	
	
	
	public UserDao<Syusers> getUserDao() {
		return userDao;
	}
	@Autowired
	public void setUserDao(UserDao<Syusers> tUserDao) {
		this.userDao = tUserDao;
	}

	public EasyuiDataGridJson showDatagrid(EasyuiDataGrid gd,Users user) {
		String hql="from Syusers where 1=1";
		String totalHql = " select count(*) " + hql;
		
		if (gd.getSort() != null) {// 设置排序
			hql += " order by " + gd.getSort() + " " + gd.getOrder();
		}
		List<Syusers> list = userDao.find(hql, gd.getPage(), gd.getRows());// 查询分页
		EasyuiDataGridJson j=new EasyuiDataGridJson();
		List<Users> users=new ArrayList<Users>();
		for(Syusers syusers:list){
			Users u=new Users();
			BeanUtils.copyProperties(syusers, u);
			users.add(u);
		}
		j.setTotal(userDao.count(totalHql));// 设置总记录数
		j.setRows(users);
		return j;
	}
	
	public Users addUser(Users users) {
		users.setId(UUID.randomUUID().toString());
		users.setPassword(Encrypt.e(users.getPassword()));
		if (users.getCreatedatetime() == null) {
			users.setCreatedatetime(new Date());
		}
		if (users.getModifydatetime() == null) {
			users.setModifydatetime(new Date());
		}
		Syusers syuser = new Syusers();
		BeanUtils.copyProperties(users, syuser);
		userDao.save(syuser);

		return users;
	}
	
	public boolean del(String ids) {
		String[] sid=ids.split(",");
		boolean falg=false;
		try {
			for(int i=0;i<sid.length;i++){
				Syusers u=userDao.get(Syusers.class,sid[i]);
				userDao.delete(u);
			}
			falg=true;
		} catch (Exception e) {
			falg=false;
		}
		return falg;
	}
	
	public Users update(Users user) {
		Syusers u=new Syusers();
		user.setPassword(Encrypt.e(user.getPassword()));
		BeanUtils.copyProperties(user, u);
		userDao.update(u);
		
		return user;
	}

}
